#coding=utf-8

from .model import OrgInfo, Organ
from .utils import AuthPassword

# register organ
def org_register(login_name, password):
    org_obj = Organ.all_objects().filter(login_name = login_name).select()

    if org_obj:
        return False, 'registered.'
    # gen password
    sec_password = AuthPassword(password)

    org_model = Organ()
    org_model.login_name = login_name
    org_model.password = sec_password.password

    org_model.save()

    return True, 'success.'

# organ login
def get_org_by_name_pwd(login_name, password):
    org_obj = Organ.all_objects().filter(login_name = login_name).select()

    if not org_obj:
        return None, 'user name not exists.'

    sec_password = AuthPassword(password)
    ret = sec_password.verify(org_obj.password)

    if not ret:
        return None, 'user password is not right.'

    return {
        'user_id': org_obj.id,
        'login_name': org_obj.login_name
    }, 'success'

# update password
def update_password(login_name, password):
    org_obj = Organ.all_objects().filter(login_name = login_name).select()

    if not org_obj:
        return False, 'user name not exists.'

    sec_password = AuthPassword(password)
    ret = sec_password.verify(org_obj.password)

    if not ret:
        return False, 'user password is not right.'

# get profile
def get_org_info(user_id):
    if not user_id:
        return False, 'user_id is null.'

    org_info = OrgInfo.all_objects().filter(org_id = user_id).select()

    if not org_info:
        return False, 'user info is null.'

    return {
        'org_name': org_info['org_name'],
        'contact_name': org_info['contact_name'],
        'contact_wechat': org_info['contact_wechat'],
        'contact_qq' : org_info['contact_qq'],
        'contact_email': org_info['contact_email'],
        'contact_phone': org_info['contact_phone'],
        'contact_telephone': org_info['contact_telephone'],
        'location': org_info['location'],
        'org_intro': org_info['org_intro'],
        'org_detail': org_info['org_detail'],
    }, 'success'

# save profile
def save_org_info(organ_info):
    if not organ_info:
        return False, 'user info is null.'
    user_id = organ_info.get('user_id')
    if not user_id:
        return False, 'save user info when user id is null.'
    this_organ_info = OrgInfo()
    # put organ info to model
    this_organ_info.org_id = user_id
    this_organ_info.org_name = organ_info['org_name']
    this_organ_info.contact_name = organ_info['contact_name']
    this_organ_info.contact_wechat = organ_info['contact_wechat']
    this_organ_info.contact_qq = organ_info['contact_qq']
    this_organ_info.contact_email = organ_info['contact_email']
    this_organ_info.contact_phone = organ_info['contact_phone']
    this_organ_info.contact_telephone = organ_info['contact_telephone']
    this_organ_info.location = organ_info['location']
    this_organ_info.org_intro = organ_info['org_intro']
    this_organ_info.org_detail = organ_info['org_detail']

    this_organ_info._save_create()

    return True, 'save user info success.'